/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.ylz.sjzx.sio.sdk.client.session;

import com.corundumstudio.socketio.SocketIOClient;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ylz.sjzx.sio.common.core.enums.Role;
import com.ylz.sjzx.sio.common.core.enums.SocketModel;
import com.ylz.sjzx.sio.sdk.client.core.IMNioBase;
import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;
import java.util.Objects;

/**
 * IoSession包装类,集群时 将此对象存入表中
 */
@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class IMSession implements Serializable {

    private transient static final long serialVersionUID = 1L;
    /**
     * 2021.2.23 新增session连接状态
     * session状态位
     * 0 物理连接状态(Session初始状态)
     * 1 逻辑连接状态
     * 2 逻辑断开状态
     * 3 物理断开状态(物理断开之后Session已经不存在)
     */
    public static int STATE_CONNECT_SOCKET = 0;
    public static int STATE_CONNECT_SESSION = 1;
    public static int STATE_DISCONNECT_SESSION = 2;
    public static int STATE_DISCONNECT_SOCKET = 3;
    @JsonIgnore
    private transient SocketIOClient socketIOClient;
    @JsonIgnore
    private transient Channel channel;
    private SocketModel socketModel;
    /**
     * session在本台服务器上的ID
     */
    private String sid;
    /**
     * 主键ID
     */
    private String userId;
    /**
     * 姓名
     */
    private String realname;
    /**
     * 个人头像
     */
    private String avatar;
    /**
     * 角色
     */
    private Role role;
    /**
     * 房间号
     */
    private String roomName;
    /**
     * 聊天室组
     */
    private String groupName;
    private String deviceType;
    /**
     * session绑定的服务器IP 未来用户处理水平服务迁移
     */
    private String hostAddress;
    private String loginTime;
    /**
     * 是临时状态 可以处理临时业务
     */
    private boolean temState;
    /**
     * socket类型socket 和mp 推流端
     */
    private String socketType;
    /**
     * true 是推流端
     */
    private boolean mpFlag;
    /**
     * true 是学生
     */
    private boolean isStudent;
    /**
     * IMSession 对象锁
     */
    @JsonIgnore
    private Object objectLock = new Object();
    private volatile int sessionState = STATE_CONNECT_SOCKET;

    public Object rightSocket() {
        if (Objects.equals(SocketModel.channel, this.socketModel)) {
            return IMNioBase.getGlobalChannel().get(this.sid);
        } else {
            return IMNioBase.getSocketIOClientMap().get(this.sid);

        }
    }

    public boolean channleOpen() {
        Object channelObj = rightSocket();
        if (channelObj == null) {
            return false;
        }
        if (Objects.equals(SocketModel.channel, this.socketModel)) {
            Channel channelObj1 = (Channel) channelObj;
            return channelObj1.isActive();
        } else {
            SocketIOClient socketIOClient = (SocketIOClient) channelObj;
            return socketIOClient.isChannelOpen();
        }
    }
}
